﻿#include "FITKCalculiXINPIOInterface.h"
#include "FITKCalculiXINPWriter.h"
#include "FITKCalculiXINPReader.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"

namespace IO
{
 

    void FITKCalculiXINPIO::setFileName(const QString& fileName)
    {
        _fileName = fileName;
    }

    QString FITKCalculiXINPIO::getComponentName()
    {
        //返回类型名
        return "CalculiXINPIO";
    }

    void FITKCalculiXINPIO::setDataObject(Core::FITKAbstractDataObject* dataObject)
    {
        _dataObject = dataObject;
    }
     
    bool FITKCalculiXINPIO::exec(const int indexPort)
    {
        if (this->isRunning()) return false;
        _success = false;
        //if (io != nullptr)  return false;
        if (_dataObject == nullptr) return false;
        if (_fileName.isEmpty()) return false;
        
        //判断indexPort 1：读取 2：写出
        switch (indexPort)
        {
        case 1:
            _io = new FITKCalculiXINPReader;
            break;
        case 2:
            _io = new FITKCalculiXINPWriter;
            break;
        default: break;
        }
        if (_io == nullptr) return false;
        
        
        connect(_io.getObjectPtr(), SIGNAL(taskFinishedSig(FITKThreadTask*)), this, 
            SLOT(ioThreadFinishedSlot()),Qt::UniqueConnection);

        _io->setFileName(_fileName);
        _io->setDataObject(_dataObject);
        _io->setResultMark(&_success);
        //在线程运行，提交到线程池
        if (_runInThread)
            _io->push2ThreadPool();
        else
        {
            _io->run();
            delete (_io.getObjectPtr());
            //不在线程运行，执行完读写之后直接触发结束信号
            emit ioThreadFinishedSig();
        }
        return true;
    }


    bool FITKCalculiXINPIO::isSuccess()
    {
        return _success;
    }

    bool FITKCalculiXINPIO::isRunning()
    {
        return  _io != nullptr;
    }

    void FITKCalculiXINPIO::setRunInThread(bool r /*= true*/)
    {
        _runInThread = r;
    }

    void FITKCalculiXINPIO::ioThreadFinishedSlot()
    {
        emit this->ioThreadFinishedSig();
 //       io = nullptr;
    }

}



